﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcSite.Models;
using System.Web.Routing;

namespace MvcSite.Controllers
{
    public class AdminController : Controller
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Pannello()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Pannello(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                User admin = GestoreUser.IsAdmin(model.UserName, model.Password);
                if (admin != null)
                {
                    Session.Add("User", admin);
                    return View("Pannello");
                }
                else
                    ModelState.AddModelError("", "The user name or password provided is incorrect.");
            }
            return View("Index");
        }

        public ActionResult Utenti()
        {
            ViewData["Utenti"] = GestoreUser.ListaUtenti(((User)Session["User"]).Username);
            
            return PartialView();
        }
    }
}
